from direct.gui.OnscreenText import *
from direct.gui.DirectGui import *
 
from panda3d.core import ConfigVariableString

from pandac.PandaModules import TextNode 
from direct.showbase.DirectObject import DirectObject
import sys
from Event import *
from Panda3DSettings import *

class States:
    PLAY = 0
    EXIT = 1
    CREATEGAME = 2
    JOINGAME = 3
    RESUME = 4
    DISCONNECT = 5
    ENDTURN = 6


class UIBuilder:
    def CreateButton(self, txt, name, cmd, position, parent=None):
        b =  DirectButton(command=cmd, textMayChange=1,frameSize = (-0.7,0.7,-0.3,0.3), scale = .3, text = txt, text_align = TextNode.ACenter, text_scale = .2, text_pos = (0,-0.1,0), extraArgs=[name])
        if(parent != None):
            b.reparentTo(parent)
        b.setPos(position[0], position[1], position[2])
        return b

    def CreateCheckbox(self, txt, name, cmd, position, boxAlign = 'left',defaultState=0, parent=None):
        c = DirectCheckButton(text=txt, scale=.05, command=cmd, pos=position, indicatorValue=defaultState, boxPlacement=boxAlign, extraArgs=[name])
        #c["indicatorValue"] = defaultState
        c.setIndicatorValue()
        if(parent != None):
            c.reparentTo(parent)
        obj = c
        return c

    def CreateFrame(self, position, size, color, parent=None):
        f = DirectFrame(frameColor=color, pos=position, frameSize=size)
        if(parent != None):
            f.reparentTo(parent)

        return f
    
    def CreateEntry(self, name, position, cmd, width,parent=None):
        e = DirectEntry(pos=position, command=cmd, extraArgs=[name], scale=.1, width=width)
        if(parent != None):
            e.reparentTo(parent)

        return e

    def ShowMessage(txt, caption):
        dialog = OkDialog(dialogName=caption,text=txt, fadeScreen=1)
        dialogShow()

    



class PauseMenu(DirectObject, UIBuilder):
    backgroundMenuColor = (.7,.7,.7,.5) #silver

    def __init__(self):
        self.StateChanged = EventBase()
        self.menuContent = self.CreateFrame((0,0,0), (.5,-.5,-.5,.5), self.backgroundMenuColor) 
        self.CreateMenu()

    def CreateMenu(self):
        self.btnResume = self.CreateButton("Resume","ResumeButton",self.Button_Click, (0,0,-.2), self.menuContent)
        self.btnBackToMenu = self.CreateButton("Back to menu", "ExitButton", self.Button_Click, (0,0,.2), self.menuContent)

    def Button_Click(self, sender):
        if sender == "ResumeButton":
            self.StateChanged(States.RESUME)
            self.hide()
        elif sender == "ExitButton":
            self.StateChanged(States.DISCONNECT)
            self.hide()


class MainMenu(DirectObject, UIBuilder):
    """Create MainMenu"""

    backgroundMenuColor = (.7,.7,.7,.5) #silver
    serverAddress = ""

    def __init__(self):
        print "init"
        self.StateChanged = EventBase()
        self.PandaSettingsChanged = EventBase()

        self.options = Panda3DSettings()

        self.menuContent = self.CreateFrame((0,0,0),(.5,-.5,-.5,.5), (0,0,0,0)) # not visible menu content frame
        #self.Hide()
        self.CreateMainMenu()
        self.CreateMultiplayerMenu()
        self.CreateOptionsMenu()
        self.CreatePlayMenu()
        self.CreateJoinGameMenu()
        self.HideAllFrames()
        self.MainMenuVisible(1)
    
    def CreateMainMenu(self):
        self.mainMenuFrame = self.CreateFrame((0,0,0), (.5,-.5,-.5,.5), self.backgroundMenuColor, self.menuContent)
        
        self.btnMPlay = self.CreateButton("Play", "ButtonPlay", self.Button_Click,(0,0,.3), self.mainMenuFrame)        
        self.btnMMultiplayer = self.CreateButton("Multiplayer", "ButtonMultiplayer", self.Button_Click, (0,0, .1), self.mainMenuFrame)
        self.btnMOptions = self.CreateButton("Options", "ButtonOptions", self.Button_Click, (0,0,-.1), self.mainMenuFrame)
        self.btnMExit = self.CreateButton("Exit", "ButtonExit", self.Button_Click, (0,0,-.3), self.mainMenuFrame) 
        
    def CreateOptionsMenu(self):
        self.menuOptionsFrame = self.CreateFrame((0,0,0), (.5,-.5,-.5,.5), self.backgroundMenuColor, self.menuContent)

        self.ckbOFullscreen = self.CreateCheckbox("Fullscreen", "FullScreenCheckBox", self.CheckBox_SelectionChanged, (0,0,0), 'right', self.options.Fullscreen,self.menuOptionsFrame)
        self.btnOAccept = self.CreateButton("Accept", "AcceptOptionButton", self.Button_Click, (-.2,0,-.3), self.menuOptionsFrame)
        self.btnOBack = self.CreateButton("Back", "BackToMainMenuButton", self.Button_Click, (.2, 0, -.3), self.menuOptionsFrame)
    
    def CreateMultiplayerMenu(self):
        self.multiplayerMenuFrame = self.CreateFrame((0,0,0), (.9,-.9,-.9,.9), self.backgroundMenuColor, self.menuContent)

        self.btnMuCreateGame = self.CreateButton("Create", "CreateServerButton", self.Button_Click, (.4,0,-.4), self.multiplayerMenuFrame)
        self.btnMuJoinGame = self.CreateButton("Join", "JoinGameButton", self.Button_Click, (-.4, 0, -.4), self.multiplayerMenuFrame)
        self.btnMuBack = self.CreateButton("Back", "BackToMainMenuButton", self.Button_Click, (0,0,-.7), self.multiplayerMenuFrame)

    def CreatePlayMenu(self):
        self.playMenuFrame = self.CreateFrame((0,0,0), (.5,-.5,-.5,.5), self.backgroundMenuColor, self.menuContent)

        self.btnPCreateServer = self.CreateButton("Create", "CreateServerButton", self.Button_Click, (-.2,0,0), self.playMenuFrame)
        self.btnPJoin = self.CreateButton("Join", "JoinGameButton", self.Button_Click, (.2,0,0), self.playMenuFrame)
        self.btnPBack = self.CreateButton("Back", "BackToMainMenuButton", self.Button_Click, (0,0,-.2), self.playMenuFrame)


    def CreateJoinGameMenu(self):
        self.joinGameMenuFrame = self.CreateFrame((0,0,0), (.5,-.5,-.5,.5), self.backgroundMenuColor, self.menuContent)

        self.txtJServerAdddress = self.CreateEntry("ServerAddressEntry", (-.35,0,0), self.Entry_TextChanged, 7, self.joinGameMenuFrame)
        self.btnJJoinGame = self.CreateButton("Join", "ConnectButton", self.Button_Click, (-.2,0,-.2), self.joinGameMenuFrame)
        self.btnJBack = self.CreateButton("Back", "ButtonMultiplayer", self.Button_Click, (.2,0,-.2), self.joinGameMenuFrame)

    def MainMenuVisible(self, visible=1):
        if(visible):
            self.mainMenuFrame.show()
        else:
            self.mainMenuFrame.hide()

    def OptionsMenuVisible(self, visible = 1):
        if(visible):
            self.menuOptionsFrame.show()
        else:
            self.menuOptionsFrame.hide()

    def PlayMenuVisible(self, visible = 1):
        if(visible):
            self.playMenuFrame.show()
        else:
            self.playMenuFrame.hide()

    def JoinGameMenuVisible(self, visible=1):
        if(visible):
            self.joinGameMenuFrame.show()
        else:
            self.joinGameMenuFrame.hide()

    def MultiplayerMenuVisible(self, visible=1):
        if visible:
            self.multiplayerMenuFrame.show()
        else:
            self.multiplayerMenuFrame.hide()

    def Show(self):
        self.menuContent.show()

    def Hide(self):
        self.menuContent.hide()


    def ShowMainMenu(self):
        self.HideAllFrames()
        self.MainMenuVisible(1)
        
            

    def Button_Click(self, sender):
        if(sender == "ButtonPlay"):
            #self.StateChanged(States.PLAY)
            self.HideAllFrames()
            self.PlayMenuVisible(1)
        elif sender == "ButtonMultiplayer":
            self.HideAllFrames()
            self.MultiplayerMenuVisible(1)
        elif(sender == "ButtonExit"):
            self.StateChanged(States.EXIT)
        elif(sender == "ButtonOptions"):
            self.HideAllFrames()
            self.OptionsMenuVisible(1)
        elif(sender == "BackToMainMenuButton"):
            self.HideAllFrames()
            self.MainMenuVisible(1)            
        elif(sender == "AcceptOptionButton"):
            self.HideAllFrames()
            self.ShowMainMenu()
            self.PandaSettingsChanged(self.options)
        elif(sender == "JoinGameButton"):
            self.HideAllFrames()
            self.JoinGameMenuVisible(1)
        elif(sender == "ConnectButton"):
            self.StateChanged(States.JOINGAME)
        elif(sender == "CreateServerButton"):
            self.StateChanged(States.CREATEGAME)


    def Entry_TextChanged(self, text, sender):
        if(sender == "ServerAddressEntry"):
            self.serverAddress = text

    def HideAllFrames(self):
        self.MainMenuVisible(0)
        self.OptionsMenuVisible(0)
        self.PlayMenuVisible(0)
        self.JoinGameMenuVisible(0)
        self.MultiplayerMenuVisible(0)

    def CheckBox_SelectionChanged(self, status, sender):
        if(sender == "FullScreenCheckBox"):
            self.options.SetFullscreen(status == 1)



class Hud(DirectObject, UIBuilder):
    def __init__(self):
        self.contentFrame = self.CreateFrame((0,0,0), (1,-1,-1,1), (0,0,0,0))
        self.StateChanged = EventBase()
        self.btnTurn = self.CreateButton("End turn", "ButtonEndTurn", self.Button_Click, (.8,0,0), self.contentFrame)
        self.btnMerge = self.CreateButton("Merge", "ButtonMerge", self.Button_Click, (.8,0,.2), self.contentFrame)
        self.btnSplit = self.CreateButton("Split", "ButtonSplit", self.Button_Click, (.8,0,.4), self.contentFrame)

    def Button_Click(self, sender):
        if sender == "ButtonEndTurn":
            self.StateChanged(States.ENDTURN)
        